package com.liury.bbs.mapper;

import com.liury.bbs.entity.Reply;
import com.liury.bbs.entity.Title;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

@Repository
public interface ReplyMapper {
    @Select("select count(*) from reply")
    public int countreply();

    @Select("select count(*) from reply where kind_id=#{kind_id}")
    public int countreplybykind(Integer kind_id);


    @Select("select count(*) from reply where title_id=#{title_id}")
    public int countreplybytitle(Integer title_id);



    @Select("select * from reply where title_id=#{titleid} order by reply_time desc limit 1")
    @Results(id = "replyMap",value = {
            @Result(property = "user",column = "user_id",one = @One(select="com.liury.bbs.mapper.UserMapper.getById")),
            @Result(property = "user_id",column = "user_id"),
    })
    public Reply newReply(int titleid);

    @Select("select * from reply where title_id=#{titleid}")
    @ResultMap("replyMap")
    public List<Reply> getRepliesByTid(int titleid);


    @Insert("insert into reply (kind_id,title_id,user_id,content,reply_time) values(#{kind_id},#{title_id},#{user_id},#{content},now())")
    public int insert(Reply reply);

    //查看回帖
    @Select("<script>" +
            "select * from reply where 1=1\n" +
            "    <if test=\"title_id!=0 and title_id!=null\">\n" +
            "         and title_id =#{title_id}\n" +
            "    </if>\n" +
            "    <if test=\"content!=null and content!=''\">\n" +
            "         and content like concat('%',#{content},'%')\n" +
            "    </if>\n" +
            "</script>")
    @ResultMap("replyMap")
    public List<Reply> getReplies(Reply r);

    @Delete("delete from reply where id=#{id}")
    public void deleteReply(Integer id);

    @Select("select * from reply where id=#{id}")
    public Reply getReplyById(Integer id);

    @Update("update reply set content=#{content} where id=#{id}")
    public void updateReply(Reply r);

    //查看回帖
    @Select("select * from reply where title_id=#{title_id}")
    public List<Reply> findReplies(Integer title_id);

}
